package com.duwei.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duwei.dao.UserMapper;
import com.duwei.pojo.User;
import com.duwei.service.UserService;
import com.duwei.util.ResponseResult;
import com.duwei.util.User1ExcelListener;
import com.duwei.util.UserExcelListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

import java.util.List;

@RestController
@RequestMapping("/test")
public class UpLoadController {

    @Autowired
    UserService userServiceImpl;

    @Autowired
    UserMapper userMapper;
    @Resource
    RedisTemplate<String,String> redisTemplate;


    @PostMapping("/user/excel")
    public ResponseResult uploadExcel(@RequestBody MultipartFile file, HttpServletResponse response) throws IOException {
        //设置内容类型
        response.setContentType("text/csv");
        //设置响应编码
        response.setCharacterEncoding("utf-8");
        //设置文件名的编码格式,防止文件名乱码
        String fileName = URLEncoder.encode("用户信息", "UTF-8");
        //固定写法,设置响应头
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
        ExcelWriter build = EasyExcel.write(response.getOutputStream(), User.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("sheet0").build();


        long start = System.currentTimeMillis();
        UserExcelListener userExcelListener = new UserExcelListener(userServiceImpl,build,writeSheet);
        EasyExcel.read(file.getInputStream(), User.class,userExcelListener).sheet(0).doRead();

        build.finish();
        long end = System.currentTimeMillis();
        System.out.println("耗费总时间："+(end-start)/1000);
        return null;
    }

    @GetMapping("/user/excel")
    public void importExcel(HttpServletResponse response) throws IOException {

        //设置内容类型
        response.setContentType("text/csv");
        //设置响应编码
        response.setCharacterEncoding("utf-8");
        //设置文件名的编码格式,防止文件名乱码
        String fileName = URLEncoder.encode("用户信息", "UTF-8");
        //固定写法,设置响应头
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), User.class).build();
        WriteSheet sheet = EasyExcel.writerSheet("sheet0").build();

        //分页查询，写入到一个sheet
        long pageSize = 10000;
        long pages = 1;
        long sheetSize = 200000;
        long sheetPages = sheetSize/pageSize;
        long start = System.currentTimeMillis();
        while(true){
            Page<User> page = new Page<>();
            page.setCurrent(pages);
            page.setSize(pageSize);
            Page<User> userPage = userMapper.selectPage(page, null);
            List<User> records = userPage.getRecords();
            if(records.size() ==0 ) break;

            excelWriter.write(records,sheet);
            //没20万数据为一个sheet
            if(pages%sheetPages == 0){
                sheet = EasyExcel.writerSheet("sheet"+pages/sheetPages).build();
            }
            pages++;
        }
        excelWriter.finish();
        long end = System.currentTimeMillis();
        System.out.println("导出耗费总时间："+(end-start)/1000);
    }


    @PostMapping("/user/excel/1")
    public ResponseResult import1Excel(@RequestBody MultipartFile file,HttpServletResponse response) throws IOException {
        //设置内容类型
        response.setContentType("text/csv");
        //设置响应编码
        response.setCharacterEncoding("utf-8");
        //设置文件名的编码格式,防止文件名乱码
        String fileName = URLEncoder.encode("用户信息", "UTF-8");
        //固定写法,设置响应头
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

        ExcelWriter build = EasyExcel.write(response.getOutputStream(), User.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("sheet0").build();


        long start = System.currentTimeMillis();
        User1ExcelListener userExcelListener = new User1ExcelListener(userServiceImpl,build,writeSheet,redisTemplate);
        EasyExcel.read(file.getInputStream(), User.class,userExcelListener).sheet(0).doRead();

        build.finish();
        long end = System.currentTimeMillis();
        System.out.println("耗费总时间："+(end-start)/1000);
        return null;

    }








}
